package com.lgy.service.impl;

import com.lgy.beans.ProductCategory;
import com.lgy.enums.ResultEnum;
import com.lgy.exception.SellException;
import com.lgy.repository.ProductCategoryRepository;
import com.lgy.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryRepository repository;

    @Override
    public ProductCategory findOne(Integer categoryId) throws SellException {
        ProductCategory productCategory = null;
        try {
            productCategory = repository.findById(categoryId).get();
        } catch (Exception e) {
            e.printStackTrace();
            throw new SellException(ResultEnum.OPERATION_FAILURE);
        }
        return productCategory;
    }

    @Override
    public List<ProductCategory> findAll() throws SellException {
        List<ProductCategory> productCategoryList = null;
        try {
            productCategoryList = repository.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            throw new SellException(ResultEnum.OPERATION_FAILURE);
        }
        return productCategoryList;
    }

    @Override
    public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) throws SellException {
        List<ProductCategory> productCategoryList = null;
        try {
            productCategoryList = repository.findByCategoryTypeIn(categoryTypeList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SellException(ResultEnum.OPERATION_FAILURE);
        }
        return productCategoryList;
    }

    @Override
    public ProductCategory save(ProductCategory productCategory) throws SellException {
        ProductCategory result = null;
        try {
            result = repository.save(productCategory);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SellException(ResultEnum.OPERATION_FAILURE);
        }
        return result;
    }
}
